import QtQuick 1.1
import com.meego 1.0
import "CommomUtil.js" as Util
import "Modulelib.js" as ModuleUtil

Page {
    id: modulebigpic_page

    tools: toolbar_container

    orientationLock : PageOrientation.LockPortrait

    property int fontSize: 16

    property int currentPage: 1

    property int totalPage: 1

    property string photo_title: ""
    property string photo_bigPath: ""
    property int photo_imgType: 1

    BusyLoading{
        id: loading
        anchors.centerIn: parent
        visible: modulebigpic_img.status === Image.Ready
    }

    Component.onCompleted: {
        modulebigpic_page.currentPage = ModuleUtil.current_photo_currentPage;
        modulebigpic_page.totalPage = ModuleUtil.current_photo_totalCount;
        modulebigpic_page.photo_title = ModuleUtil.current_photo_title;
        modulebigpic_page.photo_bigPath = ModuleUtil.current_photo_bigPath;
        modulebigpic_page.photo_imgType = ModuleUtil.current_photo_imgType;
    }

    TitleBar{
        id: modulebigpic_title
        anchors.top: modulebigpic_page.top; anchors.left: modulebigpic_page.left
        width: modulebigpic_page.width; height: 71
        titleName: "汽车图片"
    }

    Rectangle{
        width: modulebigpic_page.width; height: 740
        anchors.top: modulebigpic_title.bottom
        color: "black"

        Rectangle{
            id: modulebigpic_rect
            width: 420; height: 310
            anchors.verticalCenter: parent.verticalCenter
            border.width: 1
            border.color: "gray"
            color: "#66666f"
            anchors.centerIn: parent
            Image {
                id: modulebigpic_img
                source: modulebigpic_page.photo_bigPath
                anchors.centerIn: parent
            }
        }

        Text{
            width: parent.width; height: 50
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 60
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: modulebigpic_page.photo_title
            font{pointSize: 15}
            color: "white"
        }
    }

    ToolBarLayout{
        id: toolbar_container
        width: modulebigpic_page.width; height: 73

        //返回
        ToolIcon {
            id: back_bt
            iconId: "toolbar-back"
            width: toolbar_container.width / 6; height: toolbar_container.height
            anchors.left: parent.left; anchors.bottom: toolbar_container.bottom
            platformStyle: toolBar_style
            onClicked: {Util.back();}
        }

        //上一页
        ToolButton{
            id: prepage_bt
            text: "上一页"
            font { family: "Helvetica"; pointSize: 16; bold: true }
            width: toolbar_container.width * 5 / 18; height: toolbar_container.height
            anchors.left: back_bt.right; anchors.bottom: toolbar_container.bottom
            platformStyle: toolBar_style
            onClicked: {prePage();}
        }

        //页码
        Text{
            id: page_text
            width: toolbar_container.width * 5 / 18; height: toolbar_container.height
            anchors.left: prepage_bt.right; anchors.bottom: toolbar_container.bottom
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: modulebigpic_page.currentPage + " / " + modulebigpic_page.totalPage
            font{bold: true; pointSize: 14}
            color: "#abb2b3"
        }

        //下一页
        ToolButton{
            id: nextpage_bt
            text: "下一页"
            font { family: "Helvetica"; pointSize: 16; bold: true }
            width: toolbar_container.width * 5 / 18; height: toolbar_container.height
            anchors.left: page_text.right; anchors.bottom: toolbar_container.bottom
            platformStyle: toolBar_style
            onClicked: {nextPage();}
        }

        ToolButtonStyle{
            id: toolBar_style
            backgroundVisible: false
            pressedBackground: "image://theme/meegotouch-button-navigationbar-button" + __invertedString + "-background-pressed"
        }
    }

    function getModuleBigPicData(currentPage){
        loading.loading();
        Util.getXmlStringByUrl(ModuleUtil.getModulepiclistUrl(modulebigpic_page.photo_imgType, currentPage, 1), modulebigpicCallback);
    }

    function modulebigpicCallback(responseXml){
        if(responseXml !== null){
            var a = responseXml.documentElement;
            var imgNum = a.childNodes[0].childNodes[1].childNodes[0].nodeValue;
            if(imgNum === 0) {
                modulebigpic_page.totalPage = 1;
            } else {
                modulebigpic_page.totalPage = imgNum;
            }
            for(var i = 0; i < a.childNodes[0].childNodes.length; i++) {
                var nodeName = a.childNodes[0].childNodes[i].nodeName;
                if(nodeName === "Photo"){
                    modulebigpic_page.photo_title = a.childNodes[0].childNodes[i].childNodes[1].childNodes[0].nodeValue;
                    modulebigpic_page.photo_bigPath = a.childNodes[0].childNodes[i].childNodes[5].childNodes[0].nodeValue;
                    break;
                }
            }
        }
        loading.finish();
    }

    function prePage(){
        if(modulebigpic_page.currentPage > 1){
            modulebigpic_page.currentPage = modulebigpic_page.currentPage - 1;
            getModuleBigPicData(modulebigpic_page.currentPage);
        }
    }

    function nextPage(){
        if(modulebigpic_page.currentPage < modulebigpic_page.totalPage){
            modulebigpic_page.currentPage = modulebigpic_page.currentPage + 1;
            getModuleBigPicData(modulebigpic_page.currentPage);
        }
    }
}
